<?php

namespace App\Models\Purchase;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class PurchasePreSettleOrder extends Model
{
    protected $table = 'purchase_pre_settle_order';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];


    /**
     *获取列表 
     */
    public static function getList(Request $request, $cpid, $manger_id = 0, $isgetall = 0)
    {
        $query = self::with(['suborder' => function ($query) use ($request) {
            //过滤子订单
            // $archive_no = $request->archive_no;
            // if ($archive_no != '') {
            //     $query->where('archive_no', 'like', '%' . $archive_no . '%');
            // }

            // $archive_spec = $request->archive_spec;
            // if ($archive_spec != '') {
            //     $query->where('archive_spec', 'like', '%' . $archive_spec . '%');
            // }
            // $steel_factory_name = $request->steel_factory_name;
            // if ($steel_factory_name != '') {
            //     $query->where('steel_factory_name', 'like', '%' . $steel_factory_name . '%');
            // }
        }])
            ->where(['cpid' => $cpid, 'is_del' => 1, 'settle_model' => 3])->orderBy('create_at', 'desc');
        if ($manger_id > 0) {
            $query->where('manger_id', $manger_id);
        }

        $audit_status = $request->audit_status;
        if (in_array($audit_status, [1, 2, 3, 4])) {
            $query->where('audit_status', $audit_status);
        }


        $pa_type = $request->pa_type;
        if (in_array($pa_type, [1, 2, 3,4])) {
            $query->where('pa_type', $pa_type);
        }

        $pa_id = intval($request->pa_id);
        if ($pa_id>0) {
            $query->where('pa_id', $pa_id);
        }

        $pa_settle_company_name = $request->pa_settle_company_name; //结算公司名称
        if ($pa_settle_company_name != '') {
            $query->where('pa_settle_company_name', 'like', '%' . $pa_settle_company_name . '%');
        }
        $company_name = $request->company_name; //供应商名称
        if ($company_name != '') {
            $query->where('company_name', 'like', '%' . $company_name . '%');
        }

        $carship_no = $request->carship_no; //车船号
        if ($carship_no != '') {
            $query->where('carship_no', 'like', '%' . $carship_no . '%');
        }


        $start_time = strtotime($request->start_time) > 0 ? strtotime($request->start_time) : intval($request->start_time);
        $end_time = strtotime($request->end_time) > 0 ? strtotime($request->end_time) : intval($request->end_time);
        if ($start_time > 0 && $end_time > 0) {
            $query->where('order_date', '>=', $start_time)->where('order_date', '<=', $end_time);
        }

        //根据子订单过滤主订单
        $warehouse_name = $request->warehouse_name;
        if ($warehouse_name != '') {
            $order_arr = PurchasePreSettleOrderSub::where('warehouse_name', 'like', '%' . $warehouse_name . '%')->where(['cpid' => $cpid])->pluck('pre_settle_order_id');
            $query->whereIn('id', $order_arr);
        }

        $archive_no = $request->archive_no;
        if ($archive_no != '') {
            $order_arr = PurchasePreSettleOrderSub::where('archive_no', 'like', '%' . $archive_no . '%')->where(['cpid' => $cpid])->pluck('pre_settle_order_id');
            $query->whereIn('id', $order_arr);
        }

        $archive_spec = $request->archive_spec;
        if ($archive_spec != '') {
            $order_arr = PurchasePreSettleOrderSub::where('archive_spec', 'like', '%' . $archive_spec . '%')->where(['cpid' => $cpid])->pluck('pre_settle_order_id');
            $query->whereIn('id', $order_arr);
        }
        $steel_factory_name = $request->steel_factory_name;
        if ($steel_factory_name != '') {
            $order_arr = PurchasePreSettleOrderSub::where('steel_factory_name', 'like', '%' . $steel_factory_name . '%')->where(['cpid' => $cpid])->pluck('pre_settle_order_id');
            $query->whereIn('id', $order_arr);
        }


        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }


    /**
     * 获取子订单
     */
    public function suborder()
    {
        return $this->hasMany(PurchasePreSettleOrderSub::class, 'pre_settle_order_id', 'id');
    }


    /**
     * 获取未结算需要计价的订单
     */
    public static function getSettlePriceOrderList()
    {
        $query = self::with(['suborder'])->where(['is_del' => 1, 'settle_model' => 3,'settle_status'=>1])
        ->where([['settle_model_start_time','<=',time()],['settle_model_end_time','>=',time()]])
        ->orderBy('create_at', 'desc');
        $list = $query->get()->toArray();
        return $list;
    }

}
